package entity

import (
	"github.com/jinzhu/gorm"
	"time"
)

const (
	YaoPingReportSignDaily        = "yao-ping-report-sign-daily"         // 今日药品风险上报总数
	YaoPingReportSign             = "yao-ping-report-sign"               // 历史药品风险上报总数
	YaoPingReportSignWatch        = "yao-ping-report-sign-watch"         // 已关注药品风险总数
	YaoPingReportSignWatchDaily   = "yao-ping-report-sign-watch-daily"   // 今日已关注药品风险总数
	YaoPingReportSignUnwatch      = "yao-ping-report-sign-unwatch"       // 已关注药品风险总数
	YaoPingReportSignUnwatchDaily = "yao-ping-report-sign-unwatch-daily" // 今日已关注药品风险总数

	QiXieReportSignDaily        = "qi-xie-report-sign-daily"         // 今日医疗器械风险上报总数
	QiXieReportSign             = "qi-xie-report-sign"               // 历史医疗器械风险上报总数
	QiXieReportSignWatch        = "qi-xie-report-sign-watch"         // 已关注医疗器械风险总数
	QiXieReportSignWatchDaily   = "qi-xie-report-sign-watch-daily"   // 已关注医疗器械风险总数
	QiXieReportSignUnwatch      = "qi-xie-report-sign-unwatch"       // 已关注医疗器械风险总数
	QiXieReportSignUnwatchDaily = "qi-xie-report-sign-unwatch-daily" // 已关注医疗器械风险总数

	UserMessageSign = "user-message-times"
)

type ReportBehavior struct {
	ValueInt   int
	ValueFloat float32
	ValueBig   int64
	Times      int
	Sign       string `gorm:"type:varchar(64);primary_key"`
	Area       string `gorm:"type:varchar(64);primary_key"`
	CreatedAt  string `gorm:"type:datetime"`
	UpdatedAt  string `gorm:"type:datetime"`
	Desc       string `orm:"size(255)"`
}

func (b *ReportBehavior) TableName() string {
	return "report_behavior"
}

func (b *ReportBehavior) BeforeCreate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("created_at", time.Now().Format("2006-01-02"))
	_ = scope.SetColumn("updated_at", time.Now().Format("2006-01-02"))
	return nil
}

func (b *ReportBehavior) BeforeSave(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	_ = scope.SetColumn("value_big", gorm.Expr("value_big + ?", b.ValueBig))
	_ = scope.SetColumn("value_float", gorm.Expr("value_float + ?", b.ValueFloat))
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}

func (b *ReportBehavior) BeforeUpdate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	_ = scope.SetColumn("value_big", gorm.Expr("value_big + ?", b.ValueBig))
	_ = scope.SetColumn("value_float", gorm.Expr("value_float + ?", b.ValueFloat))
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}

type ReportBehaviorDaily struct {
	ValueInt   int
	ValueFloat float32
	ValueBig   int64
	Times      int
	Sign       string `gorm:"type:varchar(64);primary_key"`
	Area       string `gorm:"type:varchar(64);primary_key"`
	Daily      string `gorm:"type:date;primary_key"`
	CreatedAt  string `gorm:"type:datetime"`
	UpdatedAt  string `gorm:"type:datetime"`
	Desc       string `orm:"size(255)"`
}

func (b *ReportBehaviorDaily) TableName() string {
	return "report_behavior_daily"
}

func (b *ReportBehaviorDaily) BeforeCreate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("created_at", time.Now().Format("2006-01-02"))
	_ = scope.SetColumn("updated_at", time.Now().Format("2006-01-02"))
	return nil
}

func (b *ReportBehaviorDaily) BeforeSave(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	_ = scope.SetColumn("value_big", gorm.Expr("value_big + ?", b.ValueBig))
	_ = scope.SetColumn("value_float", gorm.Expr("value_float + ?", b.ValueFloat))
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}

func (b *ReportBehaviorDaily) BeforeUpdate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	_ = scope.SetColumn("value_big", gorm.Expr("value_big + ?", b.ValueBig))
	_ = scope.SetColumn("value_float", gorm.Expr("value_float + ?", b.ValueFloat))
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}

type MessageBehavior struct {
	ValueInt int
	Sign     string `gorm:"type:varchar(64);primary_key"`
	UserId   uint   `gorm:"type:int(10);primary_key"`
}

func (b *MessageBehavior) TableName() string {
	return "message_behavior"
}

func (b *MessageBehavior) BeforeUpdate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	return nil
}

func (b *MessageBehavior) BeforeSave(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("value_int", gorm.Expr("value_int + ?", b.ValueInt))
	return nil
}

type ReportNoticeBehavior struct {
	Area string `gorm:"type:varchar(64);primary_key"`
	Type     int    `gorm:"type:int(11);primary_key"`
	Times    int
	Status   int
}

func (b *ReportNoticeBehavior) TableName() string {
	return "report_notice"
}

func (b *ReportNoticeBehavior) BeforeSave(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}

func (b *ReportNoticeBehavior) BeforeUpdate(scope *gorm.Scope) (err error) {
	_ = scope.SetColumn("times", gorm.Expr("times + ?", b.Times))
	return nil
}
